<template>
  <el-dialog
    :title="formData.id ? '编辑' :'新增'"
    :visible.sync="visibleDialog"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="80px">
      {foreach $columns as $key=>$vo }
      {if $vo.is_show}
      <el-form-item label="{$vo.column_comment_actual}" prop="{$vo.column_name}">
        {if $vo.data_type=='varchar' && $vo.character_maximum_length!=null}
        {if $vo.character_maximum_length>30}
        <el-input v-model="formData.{$vo.column_name}" type="textarea" rows="5" maxlength="{$vo.character_maximum_length}" show-word-limit/>
        {else /}
        <el-input v-model="formData.{$vo.column_name}" maxlength="{$vo.character_maximum_length}" show-word-limit/>
        {/if}
        {else /}
        <el-input v-model="formData.{$vo.column_name}" />
        {/if}
      </el-form-item>
      {/if}
      {/foreach}
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visibleDialog=false">取消</el-button>
      <el-button type="primary" :loading="confirmLoading" @click="submit">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import * as mainService from '@/api/{$model}'
import { Message } from 'element-ui'

export default {
  name: 'Edit',
  props: {
    row: {
      type: Object,
      default() {
        return {}
      }
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {},
      confirmLoading: false,
      rules:{
    {foreach $columns as $key=>$vo}
{if $vo.is_show}
    {$vo.column_name}:[
      {if $vo.is_nullable=='NO'}
      { required: true, message: '请输入{$vo.column_comment_actual}', trigger: 'blur' },
      {/if}
  ],
      {/if}
    {/foreach}
      }
    }
  },
  computed: {
    visibleDialog: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  watch: {
    row(val) {
      this.formData = Object.assign({}, val)
    }

  },
  mounted() {

  },
  methods: {
    /**
     * 提交修改
     */
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.confirmLoading = true
          const api = this.formData.id ? mainService.update(this.formData) : mainService.add(this.formData)
          api.then(res => {
            this.confirmLoading = false
            this.visibleDialog = false
            this.$emit('confirm')
            Message({
              message: res.message,
              type: 'success'
            })
          }).catch(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
